package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.Comment;
import cn.wolfcode.trip.base.domain.Image;
import cn.wolfcode.trip.base.domain.Label;
import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.mapper.CommentMapper;
import cn.wolfcode.trip.base.mapper.ImageMapper;
import cn.wolfcode.trip.base.mapper.LabelMapper;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.ICommentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.StringTokenizer;

/**
 * @Auther: Ian
 * @Date: 2019/4/15
 * @Description: cn.wolfcode.trip.base.service.impl
 * @Version: 1.0
 */
@Service
public class CommentServiceImpl implements ICommentService {
    private final CommentMapper mapper;
    private final LabelMapper labelMapper;
    private final ImageMapper imageMapper;

    @Autowired
    public CommentServiceImpl(CommentMapper mapper, LabelMapper labelMapper, ImageMapper imageMapper) {
        this.mapper = mapper;
        this.labelMapper = labelMapper;
        this.imageMapper = imageMapper;
    }

    @Override
    public PageInfo<Comment> query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize(), "last_comment_time desc");
        List<Comment> list = mapper.selectAll(qo);
        return new PageInfo<>(list);
    }

    @Override
    public void saveOrUpdate(Comment entity) {
        entity.setLastCommentTime(new Date());
        //设置用户id
        if (entity.getUserId() != null) {
            User user = new User();
            user.setId(entity.getUserId());
            entity.setUser(user);
        }
        //保存评论
        mapper.insert(entity);
        //获取评论id
        Long id = entity.getId();
        //维护评论和攻略关系
        mapper.updateStratagyRelation(id,entity.getStratagyId());
        //获取用户传入的图片和标签
        String image = entity.getImage();
        String[] imgs = image.split(",");
        //将图片写入数据库
        for (String img : imgs) {
            if (StringUtils.hasLength(img) && !("undefined".equals(img))) {
                Image image1 = new Image();
                image1.setUrl(img);
                imageMapper.insert(image1);
                Long imgId = image1.getId();
                mapper.updateImgRelation(id, imgId);
            }
        }
        //将标签写入数据库
        String label1 = entity.getLabel();
        String[] lb = label1.split(",");
        List<String> labels=new ArrayList<>();
        for (String l : lb) {
            if (StringUtils.hasLength(l) && !("undefined".equals(l))) {
                if(l.contains(" ")) {//如果含空格则分词后再加入集合
                    StringTokenizer st = new StringTokenizer(l);
                    while (st.hasMoreElements()) {
                       labels.add(st.nextToken());
                    }
                }else {//不含空格则直接加入集合
                    labels.add(l);
                }
            }
        }
        for (String label2 : labels) {
            Label label = new Label();
            label.setName(label2);
            labelMapper.insert(label);
            Long labelId = label.getId();
            mapper.updateLabelRelation(id, labelId);
        }

    }

    @Override
    public Comment selectById(Long id) {
        return mapper.selectByPrimaryKey(id);
    }
}
